#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@file:log.py
@time:2017/7/21 0021 9:19
"""
import os
import logging
from ..config import settings

def check_path_exit(log_abs_file):
    log_path = os.path.split(log_abs_file)[0]
    if not os.path.exists(log_path):
        os.mkdir(log_path)
def write_error_log(message):
    error_log_path = settings.ERROR_LOG_FILE
    # 创建一个handler，用于写入日志文件
    file_1_1 = logging.FileHandler(error_log_path,'a',encoding='utf-8')
    # 指定日志输出格式
    fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s")
    file_1_1.setFormatter(fmt)
    # 获取logger实例，设置日志级别
    logger1 = logging.Logger('error_log',level=logging.ERROR)
    # 为logger添加的日志处理器
    logger1.addHandler(file_1_1)

    check_path_exit(error_log_path)
    logger1.error(message)

def write_run_log(message):
    error_log_path = settings.ERROR_LOG_FILE
    file_1_1 = logging.FileHandler(error_log_path,'a',encoding='utf-8')
    fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - &(levelname)s - %(module)s: %(message)s")
    file_1_1.setFormatter(fmt)
    logger1 = logging.Logger(file_1_1)
    logger1.addHandler(file_1_1)
    check_path_exit(error_log_path)
    logger1.info(message)